package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @author atguigu-mqx
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public Map<String, Object> getItem(Long skuId) {
        //  获取数据
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        HashMap<String, Object> map = new HashMap<>();
        //  获取商品的价格
        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
        //  获取分类名称登录数据
        BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
        //  获取规则参数： 平台属性 平台属性值
        List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
        //  点击切换
        Map idsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
        String strJson = JSON.toJSONString(idsMap); // {"3716|3718":"18","3717|3718":"17"}

        //  获取到销售属性+销售属性值并锁定
        List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
        //  获取海报信息！
        List<SpuPoster> spuPosterList = productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());

        //  使用拉姆达表达式
        List<Map<String, String>> skuAttrList = attrList.stream().map(baseAttrInfo -> {
            //  创建一个map
            Map<String, String> attrMap = new HashMap<>();
            attrMap.put("attrName", baseAttrInfo.getAttrName());
            //  不懂了！
            attrMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
            return attrMap;
        }).collect(Collectors.toList());
        //  key =  页面获取数据时，需要绑定的key！

        map.put("skuInfo",skuInfo);
        map.put("categoryView",categoryView);
        map.put("spuSaleAttrList",spuSaleAttrList);
        map.put("valuesSkuJson",strJson);
        map.put("price",skuPrice);
        map.put("spuPosterList",spuPosterList);
        //  规格：需要获取的是 attrName, attrValue
        map.put("skuAttrList",skuAttrList);
        //  返回数据
        return map;
    }
}
